package com.ziyun.pop.rest.module.mall.order;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.service.common.constant.AppType;
import com.service.common.model.PageResult;
import com.service.order.api.OrderService;
import com.service.order.constant.OrderResult;
import com.service.order.model.cart.OrderAddCartDto;
import com.service.order.model.order.OrderCancelParamDto;
import com.service.order.model.order.OrderFilesParamDto;
import com.service.order.model.order.OrderInfoRequestVo;
import com.service.order.model.order.OrderMessageParamDto;
import com.service.order.model.order.OrderMessageVO;
import com.service.order.model.order.PayOrderResultDto;
import com.service.order.model.order.QueryOrderParamDto;
import com.service.order.model.order.SaveOrderParamDto;
import com.service.order.model.order.SaveOrderRequestVO;
import com.service.order.model.order.SettlementParamDto;
import com.service.order.model.order.SettlementResultVo;
import com.service.ucenter.api.UserAccessoryService;
import com.service.ucenter.constant.UcenterResult;
import com.service.ucenter.model.accessory.UserAccessoryParamDto;
import com.service.ucenter.model.accessory.UserAccessoryResultDto;
import com.ziyun.pop.common.utils.UserUtils;

import io.renren.common.utils.R;
import io.renren.modules.sys.entity.SysUserEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;

@Api(value = "MallOrderRestApi", tags = "商城订单服务")
@RestController
@RequestMapping("popApi/mall/order/")
public class MallOrderRestApi {
	private static AppType appType = AppType.tuwen_bao;

	@Autowired
	private OrderService orderService;

	@Autowired
	private UserAccessoryService accessoryService;

	@ApiOperation(value = "获取订单列表", notes = "")
	@ApiResponses(value = @ApiResponse(code = 200, message = "订单列表", response = OrderInfoRequestVo.class))
	@PostMapping("/listOrderInfo")
	public R listOrderInfo(@RequestBody QueryOrderParamDto orderQueryParamDto) {
		Long mallUserId = UserUtils.getMallUserId();
		if (mallUserId == null) {
			return R.errorNoMallUserId();
		}

		OrderResult<PageResult<OrderInfoRequestVo>> orderResult = orderService.listOrderInfo(appType, mallUserId,
				orderQueryParamDto);
		if (R.isError(orderResult.getCode())) {
			return R.error(orderResult.getCode(), orderResult.getMessage());
		}
		return R.apiOk(orderResult.getData());
	}

	@ApiOperation(value = "根据订单号查询订单详情", notes = "")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "orderId", value = "订单编号", required = true, paramType = "query", dataType = "String") })
	@ApiResponses(value = @ApiResponse(code = 200, message = "订单详情", response = OrderInfoRequestVo.class))
	@GetMapping("getOrderDetailed")
	public R getOrderDetailed(@RequestParam("orderId") String orderId) {
		OrderResult<OrderInfoRequestVo> orderResult = orderService.getOrderDetailed(UserUtils.getMallUserId(), orderId);
		if (R.isError(orderResult.getCode())) {
			return R.error(orderResult.getCode(), orderResult.getMessage());
		}
		return R.apiOk(orderResult.getData());
	}

	@ApiOperation(value = "立即购买")
	@ApiResponses(value = @ApiResponse(code = 200, message = "立即购买", response = SettlementResultVo.class))
	@PostMapping("/getSpecialSettlement")
	public R getSpecialSettlement(@RequestBody OrderAddCartDto orderAddCartDto) {
		Long mallUserId = UserUtils.getMallUserId();
		if (mallUserId == null) {
			return R.errorNoMallUserId();
		}
		OrderResult<SettlementResultVo> orderResult = orderService.getSpecialSettlement(appType, mallUserId,
				orderAddCartDto);
		if (R.isError(orderResult.getCode())) {
			return R.error(orderResult.getCode(), orderResult.getMessage());
		}
		return R.apiOk(orderResult.getData());
	}

	@ApiOperation(value = "订单结算页")
	@ApiResponses(value = @ApiResponse(code = 200, message = "订单结算页", response = SettlementResultVo.class))
	@PostMapping("/getSettlement")
	public R getSettlement(@RequestBody SettlementParamDto paramDto) {
		Long mallUserId = UserUtils.getMallUserId();
		if (mallUserId == null) {
			return R.errorNoMallUserId();
		}
		OrderResult<SettlementResultVo> orderResult = orderService.getSettlement(appType, mallUserId, paramDto);
		if (R.isError(orderResult.getCode())) {
			return R.error(orderResult.getCode(), orderResult.getMessage());
		}
		return R.apiOk(orderResult.getData());
	}

	@ApiOperation(value = "生成订单")
	@ApiResponses(value = @ApiResponse(code = 200, message = "新增订单", response = SaveOrderRequestVO.class))
	@PostMapping("/saveOrder")
	public R saveOrder(@RequestBody SaveOrderParamDto orderParamDto) {
		OrderResult<SaveOrderRequestVO> orderResult = orderService.saveOrder(appType, UserUtils.getMallUserId(),
				orderParamDto);
		if (R.isError(orderResult.getCode())) {
			return R.error(orderResult.getCode(), orderResult.getMessage());
		}
		return R.apiOk(orderResult.getData());
	}

	@ApiOperation(value = "取消订单", notes = "")
	@ApiResponses(value = @ApiResponse(code = 200, message = "取消订单"))
	@PostMapping(value = "updateOrderCancel")
	public R updateOrderCancel(@RequestBody OrderCancelParamDto orderCancelParamDto) {
		OrderResult<?> orderResult = orderService.updateOrderCancel(UserUtils.getMallUserId(), orderCancelParamDto);
		if (R.isError(orderResult.getCode())) {
			return R.error(orderResult.getCode(), orderResult.getMessage());
		}
		return R.apiOk(orderResult.getData());
	}

	@ApiOperation(value = "删除订单", notes = "")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "orderId", value = "订单ID", required = true, paramType = "query", dataType = "String") })
	@ApiResponses(value = @ApiResponse(code = 200, message = "删除订单"))
	@PostMapping(value = "updateOrderDel")
	public R updateOrderDel(@RequestBody Map<String, Object> map) {
		OrderResult<?> orderResult = orderService.updateOrderDel(UserUtils.getMallUserId(), map);
		if (R.isError(orderResult.getCode())) {
			return R.error(orderResult.getCode(), orderResult.getMessage());
		}
		return R.apiOk(orderResult.getData());
	}

	@ApiOperation(value = "确认收货", notes = "")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "orderId", value = "订单ID", required = true, paramType = "query", dataType = "String") })
	@ApiResponses(value = @ApiResponse(code = 200, message = "确认收货"))
	@PostMapping(value = "updateOrderConfirmShip")
	public R updateOrderConfirmShip(@RequestBody Map<String, Object> map) {
		OrderResult<?> orderResult = orderService.updateOrderConfirmShip(UserUtils.getMallUserId(), map);
		if (R.isError(orderResult.getCode())) {
			return R.error(orderResult.getCode(), orderResult.getMessage());
		}
		return R.apiOk(orderResult.getData());
	}

	@ApiOperation(value = "上传附件", notes = "")
	@ApiResponses(value = @ApiResponse(code = 200, message = "上传附件"))
	@PostMapping(value = "updateOrderFiles")
	public R updateOrderFiles(@RequestBody OrderFilesParamDto orderFilesParamDto) {
		OrderResult<Map<Integer, Integer>> orderResult = orderService.updateOrderFiles(UserUtils.getMallUserId(),
				orderFilesParamDto);
		if (R.isError(orderResult.getCode())) {
			return R.error(orderResult.getCode(), orderResult.getMessage());
		}
		return R.apiOk(orderResult.getData());
	}

	@ApiOperation(value = "删除附件", notes = "")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "foIds", value = "附件ID", required = true, paramType = "query", dataType = "String") })
	@ApiResponses(value = @ApiResponse(code = 200, message = "删除附件"))
	@PostMapping(value = "updateOrderFilesDel")
	public R updateOrderFilesDel(@RequestBody Map<String, Object> map) {
		OrderResult<?> orderResult = orderService.updateOrderFilesDel(UserUtils.getMallUserId(), map);
		if (R.isError(orderResult.getCode())) {
			return R.error(orderResult.getCode(), orderResult.getMessage());
		}
		return R.apiOk(orderResult.getData());
	}

	@ApiOperation(value = "支付后订单信息", notes = "")
	@ApiResponses(value = @ApiResponse(code = 200, message = "支付后订单信息", response = PayOrderResultDto.class))
	@GetMapping(value = "getPayAfterOrderInfo")
	public R getPayAfterOrderInfo(@RequestParam("orderId") String orderId) {
		OrderResult<?> orderResult = orderService.getPayAfterOrderInfo(UserUtils.getMallUserId(), orderId);
		if (R.isError(orderResult.getCode())) {
			return R.error(orderResult.getCode(), orderResult.getMessage());
		}
		return R.apiOk(orderResult.getData());
	}

	@ApiOperation(value = "添加附件", notes = "通过附件信息进行处理")
	@ApiResponses(value = @ApiResponse(code = 200, message = "附件添加成功", response = UserAccessoryResultDto.class))
	@PostMapping("/addAccessory")
	public R addAccessory(@RequestBody Map<String, List<UserAccessoryParamDto>> map) {
		Long userId = UserUtils.getMallUserId();
		for (List<UserAccessoryParamDto> accessoryList : map.values()) {
			accessoryList.stream().forEach(acc -> {
				acc.setUserId(userId);
			});
		}
		UcenterResult<List<UserAccessoryResultDto>> orderResult = accessoryService.addAccessory(map);
		if (R.isError(orderResult.getCode())) {
			return R.error(orderResult.getCode(), orderResult.getMessage());
		}
		return R.apiOk(orderResult.getData());
	}

	@ApiOperation(value = "获取订单消息", notes = "")
	@ApiResponses(value = @ApiResponse(code = 200, message = "获取订单消息", response = UserAccessoryResultDto.class))
	@PostMapping(value = "/listOrderMessage")
	public R listOrderMessage(@RequestBody OrderMessageParamDto paramDto) {
		SysUserEntity user = UserUtils.getCurentUser();
		Long userId = user.getMallUserId();
		Long sellerId = user.getSellerId();
		paramDto.setSellerId(sellerId);
		OrderResult<PageResult<OrderMessageVO>> orderResult = orderService.listOrderMessage(userId, paramDto);
		if (R.isError(orderResult.getCode())) {
			return R.error(orderResult.getCode(), orderResult.getMessage());
		}
		return R.apiOk(orderResult.getData());
	}

}
